package model

import "myserver/app/types"

func CreateSysGoodsTable() {
	p := &types.SysGoods{}
	tables = append(tables, *p)
}

// 添加一个商品
func AddGoods(m *types.SysGoods) error {
	var err error
	tx := db.Begin()
	if err = tx.Create(m).Error; err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return err
}

// 根据uuid 查询
func FindGoodsByUuid(strs []string) ([]types.SysGoods, error) {
	var goods []types.SysGoods
	var err error
	tx := db.Begin()
	err = tx.Where("uuid IN ? AND is_del=?", strs, 0).Find(&goods).Error
	if err != nil {
		tx.Rollback()
	} else {
		tx.Commit()
	}

	return goods, err
}

func FindGoodsByBarcode(strs []string) ([]types.SysGoods, error) {
	var goods []types.SysGoods
	var err error
	tx := db.Begin()
	err = tx.Where("barcode IN ? AND is_del=?", strs, 0).Find(&goods).Error
	if err != nil {
		tx.Rollback()
	} else {
		tx.Commit()
	}

	return goods, err
}

// 更新商品信息
func UpDataGoods(list *[]types.SysGoods) error {
	tx := db.Begin()
	err := tx.Save(list).Error
	if err != nil {
		tx.Rollback()
	} else {
		tx.Commit()
	}
	return err

}
